<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring依赖注入：@Autowired vs @Resource深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                Spring依赖注入的艺术
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入理解 @Autowired 与 @Resource 的差异与最佳实践
            </p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-code mr-2"></i>Spring Framework
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-plug mr-2"></i>依赖注入
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-graduation-cap mr-2"></i>进阶指南
                </span>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="prose prose-lg">
                <p class="text-gray-700 leading-relaxed text-lg">
                    <span class="drop-cap">在</span>Spring框架的世界里，依赖注入是实现控制反转（IoC）的核心机制。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Autowired</code>和<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Resource</code>作为两个最常用的注入注解，它们看似相似，实则各有千秋。本文将深入剖析这两个注解的本质区别，帮助你在实际开发中做出最佳选择。
                </p>
            </div>
        </div>
    </section>

    <!-- Visual Comparison -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <h2 class="text-4xl font-bold text-center mb-12 gradient-text">核心差异一览</h2>
            
            <div class="mermaid">
                graph TD
                    A[依赖注入注解] --> B[@Autowired]
                    A --> C[@Resource]
                    
                    B --> B1[Spring框架提供]
                    B --> B2[按类型注入]
                    B --> B3[需配合@Qualifier]
                    B --> B4[支持构造器/字段/方法]
                    
                    C --> C1[Java EE标准]
                    C --> C2[按名称注入]
                    C --> C3[独立使用]
                    C --> C4[仅支持字段/方法]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>
    </section>

    <!-- Detailed Comparison -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <h2 class="text-4xl font-bold text-center mb-12">六大核心差异详解</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 差异1 -->
                <div class="card-hover bg-gradient-to-br from-purple-50 to-pink-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-purple-500 to-pink-500 text-white mb-4">
                        <i class="fas fa-globe"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">1. 来源不同</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-3"></i>
                            <span><code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">@Autowired</code> 是Spring框架的原生注解</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-pink-500 mt-1 mr-3"></i>
                            <span><code class="bg-pink-100 text-pink-800 px-2 py-1 rounded text-sm">@Resource</code> 是Java EE标准注解（JSR-250）</span>
                        </li>
                    </ul>
                </div>

                <!-- 差异2 -->
                <div class="card-hover bg-gradient-to-br from-blue-50 to-indigo-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-blue-500 to-indigo-500 text-white mb-4">
                        <i class="fas fa-search"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">2. 注入方式</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-layer-group text-blue-500 mt-1 mr-3"></i>
                            <span><code class="bg-blue-100 text-blue-800 px-2 py-1 rounded text-sm">@Autowired</code> 按类型（byType）自动装配</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tag text-indigo-500 mt-1 mr-3"></i>
                            <span><code class="bg-indigo-100 text-indigo-800 px-2 py-1 rounded text-sm">@Resource</code> 按名称（byName）自动装配</span>
                        </li>
                    </ul>
                </div>

                <!-- 差异3 -->
                <div class="card-hover bg-gradient-to-br from-green-50 to-teal-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-green-500 to-teal-500 text-white mb-4">
                        <i class="fas fa-puzzle-piece"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">3. 配合使用</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-link text-green-500 mt-1 mr-3"></i>
                            <span><code class="bg-green-100 text-green-800 px-2 py-1 rounded text-sm">@Autowired</code> 需配合@Qualifier解决歧义</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-unlink text-teal-500 mt-1 mr-3"></i>
                            <span><code class="bg-teal-100 text-teal-800 px-2 py-1 rounded text-sm">@Resource</code> 可独立使用，通过name属性指定</span>
                        </li>
                    </ul>
                </div>

                <!-- 差异4 -->
                <div class="card-hover bg-gradient-to-br from-yellow-50 to-orange-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-yellow-500 to-orange-500 text-white mb-4">
                        <i class="fas fa-box"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">4. 所属包</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-folder text-yellow-500 mt-1 mr-3"></i>
                            <span><code class="bg-yellow-100 text-yellow-800 px-2 py-1 rounded text-sm">org.springframework.beans.factory.annotation</code></span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-folder-open text-orange-500 mt-1 mr-3"></i>
                            <span><code class="bg-orange-100 text-orange-800 px-2 py-1 rounded text-sm">javax.annotation.Resource</code></span>
                        </li>
                    </ul>
                </div>

                <!-- 差异5 -->
                <div class="card-hover bg-gradient-to-br from-red-50 to-pink-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-red-500 to-pink-500 text-white mb-4">
                        <i class="fas fa-crosshairs"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">5. 作用域</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-expand-arrows-alt text-red-500 mt-1 mr-3"></i>
                            <span><code class="bg-red-100 text-red-800 px-2 py-1 rounded text-sm">@Autowired</code> 支持构造器、字段、setter方法</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-compress-arrows-alt text-pink-500 mt-1 mr-3"></i>
                            <span><code class="bg-pink-100 text-pink-800 px-2 py-1 rounded text-sm">@Resource</code> 仅支持字段、setter方法</span>
                        </li>
                    </ul>
                </div>

                <!-- 差异6 -->
                <div class="card-hover bg-gradient-to-br from-indigo-50 to-purple-50 p-8 rounded-2xl">
                    <div class="feature-icon bg-gradient-to-br from-indigo-500 to-purple-500 text-white mb-4">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">6. 可移植性</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-lock text-indigo-500 mt-1 mr-3"></i>
                            <span><code class="bg-indigo-100 text-indigo-800 px-2 py-1 rounded text-sm">@Autowired</code> Spring专属，迁移需修改代码</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-unlock text-purple-500 mt-1 mr-3"></i>
                            <span><code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">@Resource</code> 标